#Author：雨霁夜茶

import requests
import json
import re
import time
import threading

data_dict_okex={}
data_dict_bittrex={}
data_dict_all={}
keys=set()
lock = threading.RLock()
def load_okex():
    try:
        res = requests.get("https://www.okex.com/v2/spot/markets/tickers")
        lock.acquire()
        text=json.loads(res.text)
        count = len(text['data'])
        btc_price=0
        for i in range(count):
            name = text['data'][i]['symbol']
            data_value = float(text['data'][i]['last'])
            key = name.replace('_usdt', '').replace('_btc', '').lower()
            if key=='btc':
                data_dict_bittrex[key] = {'usdt': data_value, 'btc': 0, 'precent': 0}
                btc_price=data_value
                break
        for i in range(count):
            name = text['data'][i]['symbol']
            data_value=float(text['data'][i]['last'])
            key = name.replace('_usdt', '').replace('_btc', '').lower()
            if 'usdt' in name:
                key = name.replace('_usdt', '').replace('_btc', '').lower()
                keys.add(key)
                # print(name.replace('_usdt','').lower(), ':', text['data'][i]['last'])
                data_dict_okex[key]={'key':key,'usdt':data_value,'btc':0,'precent':0,}
        for i in range(count):
            name = text['data'][i]['symbol']
            data_value = float(text['data'][i]['last'])
            key = name.replace('_usdt', '').replace('_btc', '').lower()
            if 'btc' in name:
                key = name.replace('_usdt', '').replace('_btc', '').lower()
                keys.add(key)
                if data_dict_okex.get(key) != None:
                    data_dict_okex.get(key)['btc'] = data_value
                else:
                    data_dict_okex[key] = {'key': key, 'usdt': data_value * btc_price,
                                           'btc': data_value, 'precent': 0}
        # print('name:',name)
        data_dict_okex['btc']['btc'] = 1
        lock.release()
        print('下载成功：Okex')
        return data_dict_okex,keys
    except requests.exceptions.RequestException:
        print('RequestException Okex')
        return data_dict_okex, keys
    except requests.exceptions.ConnectionError:
        print('ConnectionError Okex')
        return data_dict_okex, keys
    except:
        print('Error Okex')
        return data_dict_okex, keys

if __name__ == "__main__":
    load_okex()
    for k,v in data_dict_okex.items():
        print(v)

